const mysql = require('mysql2/promise');
const fs = require('fs').promises;
const path = require('path');

async function updateDealersTable() {
  try {
    // 创建数据库连接
    const connection = await mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: '', // 请根据你的实际配置修改
      database: 'hanlinshudian'
    });

    console.log('Connected to database');

    // 添加 type 字段
    await connection.execute(`
      ALTER TABLE dealers 
      ADD COLUMN type ENUM('book', 'goods') NOT NULL DEFAULT 'book' AFTER phone
    `);

    console.log('Added type column');

    // 更新现有数据
    await connection.execute(`
      UPDATE dealers SET type = 'book' WHERE type IS NULL
    `);

    console.log('Updated existing records');

    await connection.end();
    console.log('Database update completed successfully');
  } catch (error) {
    console.error('Error updating database:', error);
    process.exit(1);
  }
}

updateDealersTable(); 